import matplotlib.pyplot as plt
from copy import deepcopy
import numpy as np


def plot_anything(data_to_plot: list, block: bool=True) -> None:
    """
    As you see, plot anything in one figure.
    data_to_plot: e.g.: [(x1_list, y1_list), (y2_list), (x3_list, y3_list), ...]
    block:        block the main program or not
    """
    data = deepcopy(data_to_plot) # preserve original data
    amount = len(data)
    if amount >= 10:
        data = data[:9]
    plt.figure(10 + amount)
    for index, each in enumerate(data):
        plt.subplot(amount, 1, index+1)
        if type(each) == tuple:
            if len(each) == 1:
                plt.plot(each[0])
            elif len(each) == 2:
                plt.plot(each[0], each[1])
        elif type(each) == list:
            plt.plot(each)
        else:
            raise TypeError("things in data_to_plot must be tuple!")
    plt.show(block=block)



def left_four_pass_five_round(num: float) -> int:
    """四舍五入"""
    temp_res = int(num)
    num *= 10
    remainder = num % 10  # get remainder
    if remainder <= 4:
        return temp_res
    else:
        return temp_res + 1



def detect_no_signal(data,sample_rate,ratio):
    length = len(data)
    for i in range(int(length/(sample_rate*5))):
        sub_data = data[i*sample_rate:(i+1)*sample_rate]
        max = np.max(sub_data)
        min =np.min(sub_data)
        if max -min <ratio:
            return 0
    return 1